package edu.uit.se.slgroup.construct.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.uit.se.slgroup.construct.domain.Category;
import edu.uit.se.slgroup.construct.services.CategoryService;

@Controller
public class CategoryController {
	
	@Autowired
	CategoryService categoryService;
	
	@RequestMapping(value="home", method=RequestMethod.GET)
	public ModelAndView home() {
		return new ModelAndView("home", "utftest", "trần Hữu Linh");
	}
	
	@RequestMapping("insert-category-form")
	public ModelAndView getForm(@ModelAttribute Category category) {
		System.out.println("get form insert category");
		return new ModelAndView("insert-category");	//insert-category.jsp			
	}
	
	@RequestMapping("insertCategory")
	public ModelAndView insertCategory(@ModelAttribute Category category) {
		categoryService.insertCategory(category);
		return new ModelAndView("redirect:category-list-form");
	}
	
	@RequestMapping("category-list-form")
	public ModelAndView getCategoryList() {
		List<Category> categoryList = categoryService.getCategoryList();
		System.out.println(" in get category list");
		for (Category category : categoryList) {
			System.out.println(category.getName());
		}
		return new ModelAndView("category-list", "categoryList", categoryList);		//category-list.jsp
	}
	
	@RequestMapping("deleteCategory")
	public ModelAndView deleteCategory(@RequestParam int id) {
		categoryService.deleteCategory(id);
		return new ModelAndView("redirect:category-list-form");
	}
	
	@RequestMapping("edit-category-form")
	public ModelAndView editCategory(@RequestParam int id, @ModelAttribute Category category) {
		Category categoryObject = categoryService.getCategoryById(id);
		return new ModelAndView("edit-category", "categoryObject", categoryObject);		//edit-category.jsp
	}
	
	@RequestMapping("updateCategory")
	public ModelAndView updateCategory(@ModelAttribute Category category) {
		categoryService.updateCategory(category);
		return new ModelAndView("redirect:category-list-form");
	}
	
}
